<template>
    <div class="app-container">
        <el-form :model="form" :rules="rules" ref="form" label-width="100px">
            <el-form-item label="套餐标题" prop="combos_name">
                <el-input v-model="form.combos_name" placeholder="请输入文章标题"></el-input>
            </el-form-item>
            <el-row :gutter="24">
                <el-col :span="12">
                    <el-form-item label="套餐原价" prop="combos_ago_price">
                        <el-input v-model="form.combos_ago_price" placeholder="请输入套餐原价"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="套餐优惠价" prop="combos_now_price">
                        <el-input v-model="form.combos_now_price" placeholder="请输入套餐优惠价"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="24">
                <el-col :span="12">
                    <el-form-item label="排序" prop="order">
                        <el-input v-model="form.order" placeholder="请输入套餐顺序(整数)"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="首页展示图" prop="combos_images1">
                <el-upload
                    class="upload-demo"
                    drag
                    :limit="2"
                    :action="importFileUrl"
                    :data="upLoadData"
                    :on-success="handleAvatarSuccess1"
                    :on-error="handleError"
                    :before-upload="beforeAvatarUpload"
                    :on-change="handleChange">
                    <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过5MB(建议尺寸458*175)</div>
                </el-upload>
            </el-form-item>
            <el-form-item label="详情内容图" prop="combos_images2">
                <el-upload
                        class="upload-demo"
                        drag
                        :limit="2"
                        :action="importFileUrl"
                        :data="upLoadData"
                        :on-success="handleAvatarSuccess2"
                        :on-error="handleError"
                        :before-upload="beforeAvatarUpload"
                        :on-change="handleChange">
                    <img v-if="imageUrl2" :src="imageUrl2" class="avatar">
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过5MB(建议尺寸250*350)</div>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <!-- <el-button type="primary" @click="toLink('/article/index')">返回</el-button> -->
                <el-button type="primary" @click="submit('form')">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script type="text/javascript">
import { quillEditor } from 'vue-quill-editor';
import 'qiniu-js/dist/qiniu.min.js';
export default {
    components: {
        'quill-edit': quillEditor
    },
    data() {
        return {
            importFileUrl: 'http://upload-z2.qiniup.com',
            upLoadData: {
                token: '',
            },
            imageUrl1:'',
            imageUrl2:'',
            form: {
                combos_ago_price: '',
                combos_name: '',
                combos_now_price: '',
                order: '',
                combos_images1: '',
                combos_images2: '',
            },
            options: {
                categories: [],
                recommends: [],
                status: []
            },
            thumbnailFileList: [],
            rules: {
                combos_ago_price: [
                    { required: true, message: '请输入套餐原价', trigger: 'blur' }
                ],
                combos_name: [
                    { max: 50, message: '长度不得超过 50 个字符', trigger: 'blur' }
                ],
                combos_now_price: [
                    { max: 25, message: '长度不得超过 25 个字符', trigger: 'blur' }
                ],
                combos_images1: [
                    { max: 255, message: '长度不得超过 255 个字符', trigger: 'blur' }
                ],
                order: [
                    { type: 'number', message: '排序必须为数字' , trigger: 'blur', transform(value) {
                            return Number(value);
                        }}
                ],
            },
            editorOption: {
                modules: {
                    toolbar: [
                        ['bold', 'italic', 'underline', 'strike'],
                        ['blockquote', 'code-block'],
                        [{ 'header': 1 }, { 'header': 2 }],
                        [{ 'list': 'ordered' }, { 'list': 'bullet' }],
                        [{ 'script': 'sub' }, { 'script': 'super' }],
                        [{ 'indent': '-1' }, { 'indent': '+1' }],
                        [{ 'direction': 'rtl' }],
                        [{ 'size': ['small', false, 'large', 'huge'] }],
                        [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
                        [{ 'color': [] }, { 'background': [] }],
                        [{ 'font': [] }],
                        [{ 'align': [] }],
                        ['clean'],
                        ['link', 'image', 'video']
                    ]
                }
            },
            uploadHeaders: {
                'X-CSRF-TOKEN': window.laravelCsrfToken
            },
        }
    },
    mounted() {
        this.getUptoken();
    },
    methods: {
        getUptoken() {
            let _this = this;
            axios.get('api/qiniu/token').then(response => {
                let { status, data, message } = response.data;
                _this.upLoadData.token= data.uptoken;
            });
        },
        submit(formName) {
            let _this = this;
            _this.$refs[formName].validate((valid) => {
                if (valid) {
                    axios.post('/backend/combos/create', { 'data': _this.form }).then(response => {
                        let {status, data, message} = response.data;
                        if (!status) {
                            _this.$message.error(message);
                            return false;
                        }
                        _this.$message.success(message);
                        _this.$router.push({ path: '/brand/combo' });
                    });
                }
            });
        },
        handleAvatarSuccess1(res, file) {
            //上传成功后在图片框显示图片
            this.imageUrl1 ='http://orrs4ojbz.bkt.clouddn.com/'+ res.key;
            this.form.combos_images1=res.key;
        },
        handleAvatarSuccess2(res, file) {
            //上传成功后在图片框显示图片
            this.imageUrl2 ='http://orrs4ojbz.bkt.clouddn.com/'+ res.key;
            this.form.combos_images2=res.key;
        },
        handleError(res) {
            //显示错误
            //console.log(res)
        },
        handleChange(file,fileList) {
            //限制数量
            if(fileList.length>1){
                fileList.splice(0,1);
            }
        },
        beforeAvatarUpload(file) {
            //console.log(file);

            let _this = this,
                fileType = file.type,
                fileSize = file.size / 1024,
                truePictureType = ['image/jpeg', 'image/jpg', 'image/png', 'image/x-png'];
            if (truePictureType.indexOf(fileType) == -1) {
                _this.$message.error('请上传正确的图片的格式（jpg/png）');
                return false;
            }
            if (fileSize > 5120) {
                _this.$message.error('上传的图片大小不能超过 5MB');
                return false;
            }
            return true;
        },
        thumbnailChange(file, fileList) {
            this.thumbnailFileList = fileList.slice(-1);
        }
    }
}
</script>